﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Lyon.Utility
{
    /// <summary>
    /// 字符串路径。
    /// </summary>
    [System.Diagnostics.DebuggerDisplay("FullName = {FullName}")]
    [Serializable]
    public class StringPath
    {
        char separator = '\\';
        string _Name;
        string _ParentFullName;
        /// <summary>
        /// 初始化字符串路径。
        /// </summary>
        /// <param name="fullName"></param>
        public StringPath(string fullName)
        {
            Guard.ArgumentNotNullOrEmptyString(fullName, "fullName");

            fullName = fullName.Trim(separator);
            int index = fullName.LastIndexOf(separator);
            if (index > 0)
            {
                _Name = fullName.Remove(0, index + 1);
                _ParentFullName = fullName.Remove(index);
            }
            else
            {
                _Name = fullName;
                _ParentFullName = null;
            }
        }
        /// <summary>
        /// 初始化字符串路径。
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="parent">父级字符串路径</param>
        public StringPath(string name, StringPath parent)
        {
            Guard.ArgumentNotNullOrEmptyString(name, "name");
            _Name = name;
            if (parent != null)
                _ParentFullName = parent.FullName;
        }
        /// <summary>
        /// 名称。
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }
        }
        /// <summary>
        /// 全名。
        /// </summary>
        public string FullName
        {
            get
            {
                if (String.IsNullOrEmpty(_ParentFullName))
                    return _Name;
                return String.Concat(_ParentFullName, separator, _Name);
            }
        }
        /// <summary>
        /// 父级路径。
        /// </summary>
        public StringPath Parent
        {
            get
            {
                if (String.IsNullOrEmpty(_ParentFullName))
                    return null;
                return new StringPath(_ParentFullName);
            }
        }
        /// <summary>
        /// 返回一个值，该值指示此实例是否与指定的对象相等。
        /// </summary>
        /// <param name="obj">与此实例进行比较的对象。</param>
        /// <returns>如果 obj 是 <see cref="Lyon.Utility.StringPath"/> 的实例并且等于此实例的值，则为 true；否则为 false。</returns>
        public override bool Equals(object obj)
        {
            StringPath path = obj as StringPath;
            if (path == null)
                return false;
            return String.Equals(FullName, path.FullName);
        }
        /// <summary>
        /// 返回哈希值
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return FullName.GetHashCode();
        }
    }
}
